package com.baic.ibase.service.mqtt.handler;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baic.ibase.service.devices.DeviceService;
import com.baic.ibase.service.mqtt.MqttMessageStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.regex.Pattern;


@Slf4j
@Component
public class CommandAckHandler implements MqttMessageStrategy {

    String commandAckRegex = "^/ibase-device/([0-9]+)/([0-9]+)/service/command/ack$";

    Pattern commandAckPattern = Pattern.compile(commandAckRegex);


    @Autowired
    private DeviceService deviceService;

    @Override
    public boolean support(String topic) {
        return commandAckPattern.matcher(topic).matches();
    }

    @Override
    public void handle(JSONObject jsonObject) {
        deviceService.commandAck(jsonObject);
    }
}
